package com.green.service.manage.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.green.dao.manage.FunPermissionDao;
import com.green.model.manage.FunPermission;
import com.green.model.manage.Module;
import com.green.model.manage.Position;
import com.green.model.manage.Resources;
import com.green.service.common.ServiceContext;
import com.green.service.common.impl.BaseServiceImpl;
import com.green.service.manage.FunPermissionService;



/**
 * 模块权限
 * 
 */
@Repository("funPermissionService")
public class FunPermissionServiceImpl extends BaseServiceImpl implements FunPermissionService {
	@Autowired
	private FunPermissionDao funPermissionDao;
	@Override
	public int insertFunPermission(FunPermission ct) {
		return funPermissionDao.insertFunPermission(ct);
	}

	@Override
	public int updateFunPermission(FunPermission ct) {
		return funPermissionDao.updateFunPermission(ct);
	}

	@Override
	public int deleteFunPermission(Map<String, Object> param) {
		return funPermissionDao.deleteFunPermission(param);
	}

	@Override
	public FunPermission getFunPermissionById(String id) {
		return funPermissionDao.getFunPermissionById(id);
	}

	@Override
	public List<FunPermission> getFunPermissionList(Map<String, Object> param) {
		return funPermissionDao.getFunPermissionList(param);
	}

	@Override
	public int deleteFunPermission(String id,int type,String positionId) {
		int count = 0;
		if (null != id && !id.equals("")) {
			if(type==1){
				FunPermission fp=new FunPermission();
				fp.setId(UUID.randomUUID().toString());
				fp.setCreateBy(ServiceContext.getLoginPerson());
				Module module=new Module();
				module.setId(id);				
				fp.setModule(module);
				Position position=new Position();
				position.setId(positionId);
				fp.setPosition(position);
				count=this.insertFunPermission(fp);
			}else{
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("positionId", positionId);
				param.put("moduleId", id);
				count=this.deleteFunPermission(param);
			}
			
		}
		return count;
	}

	@Override
	public int saveFunPermission(String positionId,String moduleId ,String resourceList) {
		//先删除  再添加
		Map<String, Object> param = new HashMap<String, Object>();		
		param.put("positionId", positionId);	
		param.put("moduleIdList", moduleId);				
		this.deleteFunPermission(param);
		int count = 1;
		if (null != resourceList) {
			String[] idList = resourceList.split(",");
			for (String obj : idList) {				
				FunPermission fp=new FunPermission();
				fp.setId(UUID.randomUUID().toString());
				fp.setCreateBy(ServiceContext.getLoginPerson());
				Resources resource=new Resources();
				resource.setId(obj);
				fp.setResource(resource);
				Position position=new Position();
				position.setId(positionId);
				fp.setPosition(position);
				count=this.insertFunPermission(fp);
			}
		}
		return count;
	}
	
	
}
